package slhs.tempo.dataImporterUtilities;

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;

public class IOFileHandler {

    private final JFileChooser fileChooser;
    private String[] absolutePaths;
    private int numberOfFiles;
    private static final String DEFAULT_OUTPUT_FILE_NAME = "output.xls";
    private int response = JOptionPane.NO_OPTION;
    private final String[] emptyPath = { "" };

    public IOFileHandler() {
	fileChooser = new JFileChooser();
	fileChooser.setAcceptAllFileFilterUsed(false);
    }

    /*
     * Following method provides UI to select *.out files obtained as an output of Tempo
     */
    public String[] selectFile(boolean allowMultipleFileSelection, FileNameExtensionFilter filter) {
	absolutePaths = new String[0];
	fileChooser.addChoosableFileFilter(filter);
	fileChooser.setMultiSelectionEnabled(allowMultipleFileSelection);
	int loadAction = fileChooser.showOpenDialog(null);

	if (loadAction == JFileChooser.APPROVE_OPTION) {
	    File files[] = fileChooser.getSelectedFiles();
	    absolutePaths = new String[files.length];
	    numberOfFiles = files.length;
	    for (int i = 0; i < files.length; i++) {
		absolutePaths[i] = files[i].getAbsolutePath();
	    }

	} else if (loadAction == JFileChooser.CANCEL_OPTION) {
	    response = JOptionPane.showConfirmDialog(null, "Cancel File Selection?", "Warning",
		    JOptionPane.YES_NO_OPTION);
	    if (response == JOptionPane.NO_OPTION) {
		selectFile(allowMultipleFileSelection, filter);
	    } else {
		// System.exit(0);
	    }

	}
	if (absolutePaths.length == 0) {
	    return emptyPath;
	}
	System.out.println(absolutePaths);
	return absolutePaths;
    }

    /*
     * Following method provides UI to save *.excel file after import
     */

    protected void exportToExcelFile() {

	File excelFile;
	String fileName;
	FileNameExtensionFilter filter = new FileNameExtensionFilter("Excel File", "xls");
	fileChooser.addChoosableFileFilter(filter);
	fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
	int loadAction = fileChooser.showSaveDialog(null);

	if (loadAction == JFileChooser.APPROVE_OPTION) {
	    File currentSelection = fileChooser.getSelectedFile();
	    String path = currentSelection.getAbsolutePath();
	    // System.out.println(path);
	    if (currentSelection.isDirectory()) {
		fileName = path + "\\" + DEFAULT_OUTPUT_FILE_NAME;
		excelFile = new File(fileName);
	    } else {
		if (new File(path).exists()) {
		    int response = JOptionPane.showConfirmDialog(null, "Replace existing file?", "Warning",
			    JOptionPane.YES_NO_OPTION);
		    if (response == JOptionPane.YES_OPTION) {
			// overwrite existing file
			fileName = path;
			excelFile = new File(fileName);
		    } else {

			fileName = path.replaceFirst(".xls", "-New.xls");
			excelFile = new File(fileName);
		    }
		} else {
		    if (!path.endsWith(".xls"))
			path.concat(".xls");

		    fileName = path;
		    excelFile = new File(fileName);
		}

	    }
	} else if (loadAction == JFileChooser.CANCEL_OPTION) {
	    JOptionPane.showMessageDialog(null, "File will not be saved. Continue?", "Warning",
		    JOptionPane.WARNING_MESSAGE);

	}

    }

    public int getNumberOfFiles() {
	// TODO Auto-generated method stub
	return numberOfFiles;
    }

}
